
Imports System
Imports System.Data.SqlClient
Imports _2bMatesSite.Common


Namespace _2bMatesSite.BO
	''' <summary>
	''' Journal message.
	''' </summary>
	Public Class Journal
		Inherits BaseMessage
		Private journalID As Integer = -1
		Private parentID As Integer = -1
		' -1 means no parent. No parent means root journal entry
		Public Sub New(ByVal dr As SqlDataReader)
			Init(dr)
		End Sub

		Public Sub New(ByVal parentID As Integer, ByVal fromUID As Integer, ByVal subject As String, ByVal body As String)
			Me.parentID = parentID
			Me.senderUID = fromUID
			Me.subject = subject
			Me.body = body
		End Sub

		Public Sub New(ByVal fromUID As Integer, ByVal subject As String, ByVal body As String)
			Me.senderUID = fromUID
			Me.subject = subject
			Me.body = body
		End Sub

		Public Overloads Overrides ReadOnly Property ID() As Integer
			Get
				Return journalID
			End Get
		End Property

		Public ReadOnly Property ParentJournalID() As Integer
			Get
				Return parentID
			End Get
		End Property

		Public ReadOnly Property IsRoot() As Boolean
			Get
				Return parentID < 0
			End Get
		End Property

		Public Overloads Overrides Sub Delete()
			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()
			Dim tran As SqlTransaction = conn.BeginTransaction()

			Try
				Dim cmd As SqlCommand

				If parentID < 0 Then
					' It's a root journal - delete all children first
					cmd = New SqlCommand("DELETE FROM Journal WHERE ParentID=@ParentID")
					cmd.Connection = conn
					cmd.Transaction = tran
					cmd.Parameters.Add("@ParentID", journalID)
					cmd.ExecuteNonQuery()
				End If

				cmd = New SqlCommand("DELETE FROM Journal WHERE ID=@ID")
				cmd.Connection = conn
				cmd.Transaction = tran
				cmd.Parameters.Add("@ID", journalID)
				cmd.ExecuteNonQuery()

				tran.Commit()
			Catch generatedExceptionName As Exception
				tran.Rollback()
				Throw
			Finally
				conn.Close()
			End Try
		End Sub

		Public Overloads Overrides Sub Save()
			Throw New MessageException("Journal entries may not be modified. Use Send to insert a new entry.")
		End Sub

		Public Overloads Overrides Sub Send()
			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()

			Try
				Dim cmd As New SqlCommand(GetInsertSQL())
				cmd.Connection = conn

				cmd.Parameters.Add("@UID", senderUID)
				DBManager.SetNZValue(cmd, "@ParentID", parentID)
				cmd.Parameters.Add("@Subject", subject)
				cmd.Parameters.Add("@Body", body)
				cmd.Parameters.Add("@Date", DateTime.Now)

				cmd.ExecuteNonQuery()
			Finally
				conn.Close()
			End Try
		End Sub

		'
		' Private methods
		'

		Private Sub Init(ByVal dr As SqlDataReader)
			journalID = dr.GetInt32(dr.GetOrdinal("ID"))
			senderUID = dr.GetInt32(dr.GetOrdinal("UID"))
			parentID = DBManager.GetNZInt32(dr, "ParentID")
			subject = dr.GetString(dr.GetOrdinal("Subject"))
			body = dr.GetString(dr.GetOrdinal("Body"))
			dateSent = dr.GetDateTime(dr.GetOrdinal("Date"))
		End Sub

		Private Function GetInsertSQL() As String
			Dim sql As String = "INSERT INTO Journal (UID, ParentID, Subject, Body, Date)" + Environment.NewLine
			sql += "VALUES (@UID, @ParentID, @Subject, @Body, @Date)" + Environment.NewLine
			Return sql
		End Function
	End Class
End Namespace






